﻿using System;
using System.Configuration;
using System.Reflection;

namespace Hxsoft.AppV1.Module
{
	/// <summary>
	/// 应用程序类。
	/// </summary>
	public static class App
	{
		/// <summary>
		/// 应用程序上下文。
		/// </summary>
		private readonly static IAppContext context = GetAppContext();

		/// <summary>
		/// 获取应用程序上下文。
		/// </summary>
		/// <returns>应用程序上下文。</returns>
		private static IAppContext GetAppContext()
		{
			string assemblyName = ApplicationConfiguration.Configuration.AppContextAssembly;
			string className = "Hxsoft.AppV1.Module.WebAppContext";
			Assembly assembly;

			if(string.IsNullOrEmpty(assemblyName))
			{
				assembly = Assembly.GetAssembly(typeof(Users));
			}
			else
			{
				assembly = Assembly.Load(assemblyName);
				className = ApplicationConfiguration.Configuration.AppContextClassName;
			}

			return (IAppContext)assembly.CreateInstance(className);
		}

		/// <summary>
		/// 应用程序上下文。
		/// </summary>
		public static IAppContext Context
		{
			get { return context; }
		}
	}
}